package com.lingjtx.iot.network.controller;

import com.lingjtx.common.core.ResponseResult;
import com.lingjtx.common.core.model.iot.MqttRequest;
import com.lingjtx.iot.network.common.MqttPublisherGateway;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

/**
 * Iot MQ 服务指令
 */
@RestController
@RequestMapping("/cmd")
public class CommandController {

    private final MqttPublisherGateway mqttPublisherGateway;

    public CommandController(MqttPublisherGateway mqttPublisherGateway) {
        this.mqttPublisherGateway = mqttPublisherGateway;
    }

    /**
     * 下发指令
     */
    @PostMapping("/send")
    public Mono<ResponseResult<Void>> send(@RequestBody MqttRequest model) {
        return Mono.fromRunnable(() -> mqttPublisherGateway.sendToMqtt(model.getTopic(), model.getContent()))
                .then(Mono.just(ResponseResult.createBySuccess("下发成功！")));
    }
}
